﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace WeatherFrame
{

    /// <summary>
    /// The main page of the UI
    /// </summary>
    /// <remarks>
    /// TODO:        
    ///  * Sufficient images for v1.1
    ///  - Download image index from the web
    ///  - 5 day view
    ///  - Some sort of live tile (need to wait until better tile API)
    ///  - Six dots showing page position
    ///  - Reordering locations
    ///  - Vertical orientation view
    ///  - Add earth images for 'where?' page
    ///  - Link to flickr page for image owner
    ///  - Take image or select image from camera to donate
    ///  - Share app/image
    ///  - Suggest image
    ///  - Invalidate cached weather occasionally
    ///  
    /// DONE:
    ///  - Added °C/°F button
    ///  - Fixed up animation of weather details/image details
    ///  - Set default locations to include lat/long + region/country
    ///  - Fixed some issues with tombstoning
    ///  - Changed icons to use cached images for speed (trims 2-3 seconds off startup)
    ///  - Fulfil location API certification requirements (done, as far as I can tell)
    ///  - Weather details 
    ///  - Speeded up startup slightly
    ///  - Use GPS Location ('here')
    ///  - Location search
    ///  - VERSION 1.0 RELEASED
    ///  - Sufficient images for v1.0 (430 images)
    ///  - Load images in non-UI thread
    ///  - hint text for add location textblock
    ///  - Cancel/ok buttons for adding locations
    ///  - Dark border for weather icons
    ///  - #Images: 166, 208, 240, 285, 412
    ///  - Vector based weather icons - done sun rays for sun with cloud
    ///  - Application icons, large + small
    ///  - Improved splash screen image
    ///  - Cache weather against location
    ///  - Flickr terms and conditions
    ///  - World weather online terms and conditions
    ///  - Error handling
    ///  - Save state: C/F, list of locations
    ///  - Adding/removing locations
    ///  - Waiting state
    ///  </remarks>
    public partial class MainPage : PhoneApplicationPage, IDataContextChangedHandler<MainPage>
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            DataContextChangedHelper<MainPage>.Bind(this);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            App.ViewModel.LoadData();
        }



        #region IDataContextChangedHandler<MainPage> Members

        public void DataContextChanged(MainPage sender, DependencyPropertyChangedEventArgs e)
        {
            var wm = DataContext as WeatherModel;
            if (wm == null) return;
            wm.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(wm_PropertyChanged);
        }

        void wm_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedItem")
            {
                var wm = DataContext as WeatherModel;
                MainPanorama.DefaultItem = null;
                MainPanorama.DefaultItem = wm.SelectedItem;
            }
        }

        #endregion

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //if (MainPanorama.SelectedIndex != 0)
            //{
            //    App.ViewModel.SelectedItem = App.ViewModel.Items[0];
            //    e.Cancel = true;
            //    return;
            //}
            if (App.ViewModel.ShowInfo)
            {
                App.ViewModel.ShowInfo = false;
                e.Cancel = true;
                return;
            }
        }
    }
}